package com.hxx.widget.richtext.toolbar

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.hxx.widget.R

/**
 * 取色盘
 */
class RichPalette @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    private val innerRadius = 14f
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var color = Color.GREEN

    init {
        val a = context.obtainStyledAttributes(attrs, R.styleable.RichPalette)
        color = a.getColor(R.styleable.RichPalette_color, color)
        a.recycle()
    }

    private val xMode = PorterDuffXfermode(PorterDuff.Mode.XOR)
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        paint.color = color
        val centerX = width / 2f
        val centerY = height / 2f
        val radius = Math.min(centerX, centerY) / 2
        val c = canvas.saveLayer(null, null)
        canvas.drawCircle(centerX, centerY, radius, paint)
        if (isSelected){
            paint.xfermode = xMode
            canvas.drawCircle(centerX, centerY, innerRadius, paint)
            paint.xfermode = null
        }
        canvas.restoreToCount(c)
    }
}